package com.stefan.lession03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 键盘监听
 */
public class TestKeyListener {

    public static void main(String[] args) {
        new MyKeyFrame();
    }
}

class MyKeyFrame extends Frame{

    public MyKeyFrame(){
        setBounds(100,100,400,400);
        setVisible(true);

        this.addKeyListener(
                new KeyAdapter() {
                    @Override
                    public void keyTyped(KeyEvent e) {
                        super.keyTyped(e);
                    }

                    // 键盘按下
                    @Override
                    public void keyPressed(KeyEvent e) {
                        // 获得键盘按下的键，当前的码
                        int keyCode = e.getKeyCode();// 无需记录这个数值，直接使用静态属性：VK_XXX
                        System.out.println(keyCode);
                        if (keyCode == KeyEvent.VK_UP){
                            System.out.println("按下了up键");
                        }

                    }

                    @Override
                    public void keyReleased(KeyEvent e) {
                        super.keyReleased(e);
                    }
                }
        );
    }
}
